﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

using DevComponents.DotNetBar;
using PriErp.Tools.Extensions;
using PriErp.Commands;
using System.Reflection;

namespace PriErp.Frm
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class MainFrm :Office2007RibbonForm
    {
        public MainFrm()
        {
            InitializeComponent();
            //载入登陆信息
            this.lbl_Status_Info.Text = string.Format("你好<b>,{0},</b>欢迎使用本软件...", GlobalProvider.UserInfo.Name);
            this.lbl_Ver.Text = string.Format("版本号:<b>V{0}</b>", Application.ProductVersion);
            timer_Userinfo.Enabled = true;
        }

        #region Automatic Theme/Color Scheme creation based on the selected color table
        /// <summary>
        /// 应用主题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AppThemeCommand_Executed(object sender, EventArgs e)
        {
            ICommandSource source = sender as ICommandSource;
            if (source.CommandParameter is string)
            {
                eStyle style = source.CommandParameter.ToString().ToEnum<eStyle>();
                // Using StyleManager change the style and color tinting
                StyleManager.ChangeStyle(style, Color.Empty);
                if (style == eStyle.Office2007Black || style == eStyle.Office2007Blue || style == eStyle.Office2007Silver || style == eStyle.Office2007VistaGlass)
                    StartBtnMain.BackstageTabEnabled = false;
                else
                    StartBtnMain.BackstageTabEnabled = true;
                AppSetting.SetConfig("Theme", style.ToString());
                AppSetting.SetConfig("ColorTint", "");
                GlobalProvider.m_BaseStyle = style;
                GlobalProvider.sColor = Color.Empty;
            }
            else if (source.CommandParameter is Color)
            {
                GlobalProvider.sColor = (Color)source.CommandParameter;
                StyleManager.ColorTint = GlobalProvider.sColor;
                AppSetting.SetConfig("ColorTint", GlobalProvider.sColor.ToArgb().ToString());
                
            }
        }
        /// <summary>
        /// 色彩预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Color_ColorPreview(object sender, ColorPreviewEventArgs e)
        {
            StyleManager.ColorTint = e.Color;
        }
        private bool m_ColorSelected = false;
        /// <summary>
        /// 弹出颜色设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Color_ExpandChange(object sender, EventArgs e)
        {
            if (btn_Color.Expanded)
            {
                // Remember the starting color scheme to apply if no color is selected during live-preview
                m_ColorSelected = false;
            }
            else if (!m_ColorSelected)
            {
                StyleManager.ChangeStyle(GlobalProvider.m_BaseStyle, Color.Empty);
            }
        }
        /// <summary>
        /// 主题颜色改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Color_SelectedColorChanged(object sender, EventArgs e)
        {
            m_ColorSelected = true; // Indicate that color was selected for buttonStyleCustom_ExpandChange method
            btn_Color.CommandParameter = btn_Color.SelectedColor;
        }
        #endregion

        #region SuperTab 右键的关闭操作
        SuperTabItem sCurrent = null;
        /// <summary>
        /// 弹出SuperTab右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sTabCtlMain_TabStripMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && sender is SuperTabItem)
            {
                sCurrent = sender as SuperTabItem;
                if (sCurrent.CloseButtonVisible)
                {
                    this.sTabCtlMain.Select();
                    btabMenuPopup.Popup(Control.MousePosition);
                }
            }
        }
        /// <summary>
        /// Tab的各项关闭操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabCloseCommand_Executed(object sender, EventArgs e)
        {
            ICommandSource source = sender as ICommandSource;
            string parm = source.CommandParameter.ToString();
            ReginsCommand.TabCloseCommand(sTabCtlMain, sCurrent, parm);
            
        }
        #endregion

        #region 窗体载入信息和退出提示
        /// <summary>
        /// 登陆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Userinfo_Tick(object sender, EventArgs e)
        {
            LoginInfo info = new LoginInfo();
            Rectangle r = Screen.GetWorkingArea(this);
            info.Location = new Point(r.Right - info.Width, r.Bottom - info.Height);
            info.AutoClose = true;
            info.AutoCloseTimeOut = 12;
            info.AlertAnimation = eAlertAnimation.BottomToTop;
            info.AlertAnimationDuration = 300;
            info.Show(false);
            timer_Userinfo.Enabled = false;
        }
        /// <summary>
        /// 退出确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgBoxEx.Show(this,"即将退出程序,您确定要退出程序.", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                e.Cancel = true;
        }
        #endregion

        #region 开始功能
        //退出程序
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Common Methods
        /// <summary>
        /// 各功能显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ActionCommand_Executed(object sender, EventArgs e)
        {
            if (sTabCtlMain.Tabs.Count > 10)
            {
                MsgBoxEx.Show("你已经打开了10个功能选项!\r\n为确保程序运行速度请关闭部分后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                ICommandSource source = sender as ICommandSource;
                ViewNames view = source.CommandParameter.ToString().ToEnum<ViewNames>();
                UserControl item=ReginsCommand.ShowViewPal(sTabCtlMain, view, sTabCtlMain_TabStripMouseDown);
                if (item is IView)
                    (item as IView).LoadData();
            }
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        private void ShowFrom()
        {
            this.Visible = true;
            this.nofIcon_Main.Visible = false; 
        }
        #endregion

        #region notifyIcon
        /// <summary>
        /// 点击打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nofIcon_Main_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ShowFrom();
        }
        /// <summary>
        /// 显示界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStrip_Open_Click(object sender, EventArgs e)
        {
            ShowFrom();
        }

        private void MenuItemSupport_Click(object sender, EventArgs e)
        {
            PriErpCommand.GoURL("mailto:denuvead@126.com");
        }

        private void ToolStrip_About_Click(object sender, EventArgs e)
        {
            (new AboutDlg()).Show();
        }

        private int WM_SYSCOMMAND = 0x112;
        //private long SC_MAXIMIZE = 0xF030;
        private long SC_MINIMIZE = 0xF020;
        //private long SC_CLOSE = 0xF060;
        protected override void WndProc(ref Message m)
        {
            //if (m.Msg == WM_SYSCOMMAND)
            //{
            //    if (m.WParam.ToInt64() == SC_MAXIMIZE)
            //    {
            //        MessageBox.Show("MAXIMIZE ");
            //        return;
            //    }
            //    if (m.WParam.ToInt64() == SC_MINIMIZE)
            //    {
            //        MessageBox.Show("MINIMIZE ");
            //        return;
            //    }
            //    if (m.WParam.ToInt64() == SC_CLOSE)
            //    {
            //        MessageBox.Show("CLOSE ");
            //        return;
            //    }
            //}
            if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_MINIMIZE)
            {
                this.Hide();
                nofIcon_Main.Visible = true;
                nofIcon_Main.ShowBalloonTip(300);
                return;
            }

            base.WndProc(ref m);
        }
        #endregion 
    }
}
